package com.lovi.thread_;

import javax.xml.bind.SchemaOutputResolver;

/**
 * 三个售票窗口同时在卖票
 */
public class Test04 {
    public static void main(String[] args) {
//        SellTickets tickets = new SellTickets();
//        SellTickets tickets02 = new SellTickets();
//        SellTickets tickets03 = new SellTickets();
//        tickets.start();
//        tickets02.start();
//        tickets03.start();
//        SellTickets02 tickets02 = new SellTickets02();
//        new Thread(tickets02).start();
//        new Thread(tickets02).start();
//        new Thread(tickets02).start();

        SellTickets03 tickets03 = new SellTickets03();
        new Thread(tickets03).start();
        new Thread(tickets03).start();
        new Thread(tickets03).start();
        new Thread(tickets03).start();
        new Thread(tickets03).start();
        SellTickets03 tickets03_1 = new SellTickets03();
        new Thread(tickets03_1).start();
        new Thread(tickets03_1).start();
        new Thread(tickets03_1).start();

        SellTickets03 tickets03_2 = new SellTickets03();
        new Thread(tickets03_2).start();
        new Thread(tickets03_2).start();
        new Thread(tickets03_2).start();

    }
}

//继承实现
class SellTickets extends Thread {
    //整个类共享票数
    private static int tickets = 100;

    @Override
    public void run() {
        while (true) {
            if (tickets <= 0) {
                System.out.println("票卖完了");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "卖出一张票" +
                    "还剩下" + (tickets--) + "张票");
        }
    }
}

//接口实现
class SellTickets02 implements Runnable {
    //整个类共享票数
    private static int tickets = 100;

    @Override
    public void run() {
        while (true) {
            if (tickets <= 0) {
                System.out.println("票卖完了");
                break;
            }
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("窗口" + Thread.currentThread().getName() + "卖出一张票" +
                    "还剩下" + (tickets--) + "张票");
        }
    }
}

//加上锁实现
class SellTickets03 implements Runnable {
    //整个类共享票数
    private static int tickets = 300;
    final static Object lock = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (tickets <= 0) {
//                    System.out.println("票卖完了");
                    break;
                }
                System.out.println("窗口" + Thread.currentThread().getName() + "卖出一张票" +
                        "还剩下" + (--tickets) + "张票");
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
